/*****************************************************************************
#
#
******************************************************************************/
#include "EPD_2in9_A1.h"
#include "DEBUG.h"
// #include "EPD_SDCard.h"

#define DEBUG Debug

static uint8_t iLut = 0;

/******************************************************************************
function :	Software reset
parameter:
******************************************************************************/
static void EPD_2IN9_A1_Reset(void)
{
    // DEV_Digital_Write(EPD_RST_PIN, 1);
    // DEV_Delay_ms(10);
    DEV_Digital_Write(EPD_RST_PIN, 0);
    DEV_Delay_ms(100);
    DEV_Digital_Write(EPD_RST_PIN, 1);
    DEV_Delay_ms(100);
}

/******************************************************************************
function :	send command
parameter:
     Reg : Command register
******************************************************************************/
static void EPD_2IN9_A1_SendCommand(UBYTE Reg)
{
    DEV_Digital_Write(EPD_DC_PIN, 0);
    DEV_Digital_Write(EPD_CS_PIN, 0);
    DEV_Delay_us(1);
    DEV_SPI_WriteByte(Reg);
    DEV_Delay_us(1);
    DEV_Digital_Write(EPD_CS_PIN, 1);
}

/******************************************************************************
function :	send data
parameter:
    Data : Write data
******************************************************************************/

static void EPD_2IN9_A1_SendData(UBYTE Data)
{
    DEV_Digital_Write(EPD_DC_PIN, 1);
    DEV_Digital_Write(EPD_CS_PIN, 0);
    DEV_Delay_us(1);
    DEV_SPI_WriteByte(Data);
    DEV_Delay_us(1);
    DEV_Digital_Write(EPD_CS_PIN, 1);
}

static void EPD_2IN9_A1_SendData2(UBYTE *Data, UDOUBLE length)
{
    DEV_SPI_WriteByte(Data, length);
}

static void EPD_2IN9_A1_SendData2(UBYTE Data)
{
    // DEV_Digital_Write(EPD_DC_PIN, 1);
    // DEV_Digital_Write(EPD_CS_PIN, 0);
    DEV_SPI_WriteByte(Data);
    // DEV_Digital_Write(EPD_CS_PIN, 1);
}

const uint8_t EPD_2IN9_A1_LUT_Default_full[] =
    {
        //   0x32,  // command

        0x50, 0xAA, 0x55, 0xAA, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00


        // 0x50, 0xAA, 0x55, 0xAA, 0x11, 0xAA, 0x11, 0xAA, 0x11, 0xAA, 0x11, 0x00, 0x00, 0x00, 0x00,
        // 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00


        //   0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22,
        // 0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 0x88,
        // 0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51,
        // 0x35, 0x51, 0x51, 0x19, 0x01, 0x00

        
        //   0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22,
        // 0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 0x88,
        // 0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51,
        // 0x35, 0x51, 0x51, 0x19, 0x01, 0x00

//   0x66, 0xa6, 0x6a, 0x66, 0x55, 0x99, 0xaa, 0x66,
//   0x55, 0x95, 0xaa, 0xaa, 0x59, 0x55, 0xaa, 0x55,
//   0x88, 0x11, 0x11, 0x88,
//   0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
//   0x8f, 0x8f,


        
};

const uint8_t EPD_2IN9_A1_LUT_Default_part[] =
    {
        //   0x32,  // command
        0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

static void EPD_2IN9_A1_Lut(const unsigned char *lut)
{
    EPD_2IN9_A1_SendCommand(0x32);
    for (int i = 0; i < 30; i++)
    {
        EPD_2IN9_A1_SendData(lut[i]);
    }
}

/******************************************************************************
function :	Wait until the busy_pin goes LOW
parameter:
******************************************************************************/
static void EPD_2IN9_A1_ReadBusy(void)
{
    DEBUG("e-Paper busy\r\n");
    while (1)
    {
        if (DEV_Digital_Read(EPD_BUSY_PIN) == 0)
            break;
        DEV_Delay_ms(10);
    }

    DEBUG("e-Paper busy release\r\n");
}

void EPD_2IN9_A1_ReadBusy(uint32_t ms)
{
    Debug("e-Paper busy:%d\r\n", millis());
    for (uint32_t i = 0; i < ms; i += 10)
    {
        if (!DEV_Digital_Read(EPD_BUSY_PIN))
        {
            Debug("e-Paper busy release:%d\r\n", millis());
            return;
        }
        if (ms > 200)
        {
#ifndef TEST
            EPD_BUSY_attachInterrupt(LOW);
#else
            DEV_Delay_ms(10);
#endif
        }
        else
            DEV_Delay_ms(10);
    }
    Debug("e-Paper busy timeout:%d\r\n", millis());
}

/******************************************************************************
function :	Initialize the e-Paper register
parameter:
******************************************************************************/
void EPD_2IN9_A1_Init(uint8_t lut_type)
{

    EPD_2IN9_A1_Reset();

    EPD_2IN9_A1_ReadBusy(100);
    EPD_2IN9_A1_SendCommand(0x12); // SWRESET
    EPD_2IN9_A1_ReadBusy(1000);

    EPD_2IN9_A1_SendCommand(0x01); // Driver output control
    EPD_2IN9_A1_SendData((EPD_2IN9_A1_HEIGHT - 1) % 256);
    EPD_2IN9_A1_SendData((EPD_2IN9_A1_HEIGHT - 1) / 256);
    EPD_2IN9_A1_SendData(0x00);

     EPD_2IN9_A1_SendCommand(0x04); // 
    EPD_2IN9_A1_SendData(0x0E);//0x06

    // EPD_2IN9_A1_SendCommand(0x0c); // softstart
    // EPD_2IN9_A1_SendData(0xd7);
    // EPD_2IN9_A1_SendData(0xd6);
    // EPD_2IN9_A1_SendData(0x9d);

     EPD_2IN9_A1_SendCommand(0x0c); // softstart
    EPD_2IN9_A1_SendData(0xf7);
    EPD_2IN9_A1_SendData(0xf6);
    EPD_2IN9_A1_SendData(0xfd);

    //     EPD_2IN9_A1_SendData(0xf4);
    // EPD_2IN9_A1_SendData(0xf4);
    // EPD_2IN9_A1_SendData(0xf4);

    EPD_2IN9_A1_SendCommand(0x2c); // VCOM setting
    // EPD_2IN9_A1_SendData(0xa8);
    EPD_2IN9_A1_SendData(0x08);
        // EPD_2IN9_A1_SendData(0xb9);

    EPD_2IN9_A1_SendCommand(0x3a); // DummyLine
    EPD_2IN9_A1_SendData(0x1a);    // 4 dummy line per gate
        // EPD_2IN9_A1_SendData(0x1F);    // 4 dummy line per gate
    EPD_2IN9_A1_SendCommand(0x3b); // Gatetime
        EPD_2IN9_A1_SendData(0x08);    // 2us per line
    // EPD_2IN9_A1_SendData(0);    // 2us per line


    EPD_2IN9_A1_SendCommand(0x3C); // BorderWavefrom
    // EPD_2IN9_A1_SendData(0x03);
    EPD_2IN9_A1_SendData(0x01);

    EPD_2IN9_A1_SendCommand(0x11); // data entry mode
    EPD_2IN9_A1_SendData(0x03);

    EPD_2IN9_A1_SendCommand(0x44); // set Ram-X address start/end position
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendData(EPD_2IN9_A1_WIDTH / 8 - 1);

    EPD_2IN9_A1_SendCommand(0x45); // set Ram-Y address start/end position
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendData((EPD_2IN9_A1_HEIGHT - 1) % 256);
    EPD_2IN9_A1_SendData((EPD_2IN9_A1_HEIGHT - 1) / 256);

    EPD_2IN9_A1_SendCommand(0x4E); // set RAM x address count to 0;
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendCommand(0x4F); // set RAM y address count to 0X199;
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendData(0x00);

    // * different

    //   EPD_2IN9_A1_SendData(0x00);

    // EPD_2IN9_A1_SendCommand(0x3C); // BorderWavefrom
    // EPD_2IN9_A1_SendData(0x05);

    // EPD_2IN9_A1_SendCommand(0x21); //  Display update control
    // EPD_2IN9_A1_SendData(0x00);
    // EPD_2IN9_A1_SendData(0x80);

    // EPD_2IN9_A1_SendCommand(0x18); // Read built-in temperature sensor
    // EPD_2IN9_A1_SendData(0x80);

    // EPD_2IN9_A1_SendData((EPD_2IN9_A1_HEIGHT - 1) % 256);
    // EPD_2IN9_A1_SendData((EPD_2IN9_A1_HEIGHT - 1) / 256);

    // if (lut_type == EPD_2IN9_A1_PART)
    // {
    //     EPD_2IN9_A1_Lut(lut_part_bw);
    // }

    EPD_2IN9_A1_Lut(EPD_2IN9_A1_LUT_Default_full);

    // EPD_2IN9_A1_SendCommand(0x1A); // Display Update Control
    // EPD_2IN9_A1_SendData(0x64);
    // EPD_2IN9_A1_SendData(0x00);


    EPD_2IN9_A1_SendCommand(0x1B); // Display Update Control
    EPD_2IN9_A1_SendData(0x64);

    // EPD_2IN9_A1_SendCommand(0x22); // Display Update Control
    // EPD_2IN9_A1_SendData(0xc0);
    // EPD_2IN9_A1_SendCommand(0x20); // Activate Display Update Sequence
    // EPD_2IN9_A1_SendData(0xFF);

    EPD_2IN9_A1_ReadBusy(1000);
}

void EPD_2IN9_A1_Part_Init()
{

    EPD_2IN9_A1_Reset();

    EPD_2IN9_A1_ReadBusy(100);
    EPD_2IN9_A1_SendCommand(0x12); // SWRESET
    EPD_2IN9_A1_ReadBusy(100);

    EPD_2IN9_A1_SendCommand(0x01); // Driver output control
    EPD_2IN9_A1_SendData((EPD_2IN9_A1_HEIGHT - 1) % 256);
    EPD_2IN9_A1_SendData((EPD_2IN9_A1_HEIGHT - 1) / 256);
    EPD_2IN9_A1_SendData(0x00);

    EPD_2IN9_A1_SendCommand(0x0c); // softstart
    EPD_2IN9_A1_SendData(0xd7);
    EPD_2IN9_A1_SendData(0xd6);
    EPD_2IN9_A1_SendData(0x9d);

    EPD_2IN9_A1_SendCommand(0x2c); // VCOM setting
    EPD_2IN9_A1_SendData(0xa8);
    
    // * different
    EPD_2IN9_A1_SendCommand(0x3a); // DummyLine
    EPD_2IN9_A1_SendData(0x1a);    // 4 dummy line per gate
    EPD_2IN9_A1_SendCommand(0x3b); // Gatetime
    EPD_2IN9_A1_SendData(0x08);    // 2us per line

    EPD_2IN9_A1_SendCommand(0x3C); // BorderWavefrom
    EPD_2IN9_A1_SendData(0x03);

    EPD_2IN9_A1_SendCommand(0x11); // data entry mode
    EPD_2IN9_A1_SendData(0x03);

    EPD_2IN9_A1_SendCommand(0x44); // set Ram-X address start/end position
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendData(EPD_2IN9_A1_WIDTH / 8 - 1);

    EPD_2IN9_A1_SendCommand(0x45); // set Ram-Y address start/end position
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendData((EPD_2IN9_A1_HEIGHT - 1) % 256);
    EPD_2IN9_A1_SendData((EPD_2IN9_A1_HEIGHT - 1) / 256);

    // EPD_2IN9_A1_SendCommand(0x3C); // BorderWavefrom
    // EPD_2IN9_A1_SendData(0x05);

    // EPD_2IN9_A1_SendCommand(0x21); //  Display update control
    // EPD_2IN9_A1_SendData(0x00);
    // EPD_2IN9_A1_SendData(0x80);

    // EPD_2IN9_A1_SendCommand(0x18); // Read built-in temperature sensor
    // EPD_2IN9_A1_SendData(0x80);

    EPD_2IN9_A1_SendCommand(0x4E); // set RAM x address count to 0;
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendCommand(0x4F); // set RAM y address count to 0X199;
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendData(0x00);

    EPD_2IN9_A1_Lut(EPD_2IN9_A1_LUT_Default_part);

    EPD_2IN9_A1_ReadBusy(100);
}

/******************************************************************************
function :	Clear screen
parameter:
******************************************************************************/
void EPD_2IN9_A1_Clear(void)
{
    UDOUBLE Width, Height;
    Width = (EPD_2IN9_A1_WIDTH % 8 == 0) ? (EPD_2IN9_A1_WIDTH / 8) : (EPD_2IN9_A1_WIDTH / 8 + 1);
    Height = EPD_2IN9_A1_HEIGHT;

    EPD_2IN9_A1_SendCommand(0x4E); // set RAM x address count to 0;
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendCommand(0x4F); // set RAM y address count to 0X199;
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_ReadBusy(1000);

    unsigned int i;
    EPD_2IN9_A1_SendCommand(0x24); // write RAM for black(0)/white (1)
    DEV_Digital_Write(EPD_DC_PIN, 1);
    for (i = 0; i < Width * Height; i++)
    {
        // EPD_2IN9_A1_SendData(0xFF);
        EPD_2IN9_A1_SendData(0xFF);
        // EPD_2IN9_A1_SendData2(0xFF);
    }
    // EPD_2IN9_A1_SendCommand(0x26); // write RAM for black(0)/white (1)
    // DEV_Digital_Write(EPD_DC_PIN, 1);
    // for (i = 0; i < Width * Height; i++)
    // {
    //     // EPD_2IN9_A1_SendData(0x00);
    //     EPD_2IN9_A1_SendData2(0x00);
    // }
    // EPD_2IN9_A1_SendCommand(0xFF);
    // EPD_2IN9_A1_SendCommand(0x22); // Display Update Control
    // EPD_2IN9_A1_SendData(0xc4);
    // // EPD_2IN9_A1_SendData(0xFF);
    // EPD_2IN9_A1_SendCommand(0x20); // Activate Display Update Sequence
    // EPD_2IN9_A1_SendCommand(0xFF);
    // EPD_2IN9_A1_ReadBusy(20 * 1000); // 9s
    EPD_2IN9_A1_TurnOnDisplay();
}


void EPD_2IN9_A1_Clear2(void)
{
    UDOUBLE Width, Height;
    Width = (EPD_2IN9_A1_WIDTH % 8 == 0) ? (EPD_2IN9_A1_WIDTH / 8) : (EPD_2IN9_A1_WIDTH / 8 + 1);
    Height = EPD_2IN9_A1_HEIGHT;

    EPD_2IN9_A1_SendCommand(0x4E); // set RAM x address count to 0;
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendCommand(0x4F); // set RAM y address count to 0X199;
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_SendData(0x00);
    EPD_2IN9_A1_ReadBusy(1000);


    unsigned int i;
    EPD_2IN9_A1_SendCommand(0x24); // write RAM for black(0)/white (1)
    DEV_Digital_Write(EPD_DC_PIN, 1);
    for (i = 0; i < Width * Height; i++)
    {
        // EPD_2IN9_A1_SendData(0xFF);
        EPD_2IN9_A1_SendData(0x00);
        // EPD_2IN9_A1_SendData2(0xFF);
    }
    // EPD_2IN9_A1_SendCommand(0x26); // write RAM for black(0)/white (1)
    // DEV_Digital_Write(EPD_DC_PIN, 1);
    // for (i = 0; i < Width * Height; i++)
    // {
    //     // EPD_2IN9_A1_SendData(0x00);
    //     EPD_2IN9_A1_SendData2(0x00);
    // }
    // EPD_2IN9_A1_SendCommand(0xFF);
    // EPD_2IN9_A1_SendCommand(0x22); // Display Update Control
    // EPD_2IN9_A1_SendData(0xc4);
    // // EPD_2IN9_A1_SendData(0xFF);
    // EPD_2IN9_A1_SendCommand(0x20); // Activate Display Update Sequence
    // EPD_2IN9_A1_SendCommand(0xFF);
    // EPD_2IN9_A1_ReadBusy(20 * 1000); // 9s
    EPD_2IN9_A1_TurnOnDisplay();
}

/******************************************************************************
function :	Sends the image buffer in RAM to e-Paper and displays
parameter:
******************************************************************************/
void EPD_2IN9_A1_Display(unsigned char *BW_Image)
{
    UDOUBLE Width, Height;
    Width = (EPD_2IN9_A1_WIDTH % 8 == 0) ? (EPD_2IN9_A1_WIDTH / 8) : (EPD_2IN9_A1_WIDTH / 8 + 1);
    Height = EPD_2IN9_A1_HEIGHT;

    // send black data

    EPD_2IN9_A1_SendCommand(0x24);
    DEV_Digital_Write(EPD_DC_PIN, 1);
    // EPD_2IN9_A1_SendData2(BW_Image, Width * Height);

    for (int i = 0; i < Width * Height; i++)
    {
        EPD_2IN9_A1_SendData(BW_Image[i]);
    }

    // EPD_2IN9_A1_SendCommand(0x26);
    // DEV_Digital_Write(EPD_DC_PIN, 1);
    // EPD_2IN9_A1_SendData2(R_Image, Width * Height);

    // EPD_2IN9_A1_SendCommand(0x22); // Display Update Control
    // EPD_2IN9_A1_SendData(0xC7);
    // // EPD_2IN9_A1_SendData(0xF7);
    // EPD_2IN9_A1_SendCommand(0x20); // Activate Display Update Sequence
    // EPD_2IN9_A1_ReadBusy(5 * 1000);
}

/******************************************************************************
function :	Enter sleep mode
parameter:
******************************************************************************/
void EPD_2IN9_A1_Sleep(void)
{
    EPD_2IN9_A1_SendCommand(0x10); // enter deep sleep
    EPD_2IN9_A1_SendData(0x01);
    DEV_Delay_ms(100);
}

/******************************************************************************
function :	Turn On Display
parameter:
******************************************************************************/
// void EPD_2IN9_A1_TurnOnDisplay(void)
// {
//     EPD_2IN9_A1_SendCommand(0x22); // Display Update Control
//     EPD_2IN9_A1_SendData(0xF7);
//     EPD_2IN9_A1_SendCommand(0x20); // Activate Display Update Sequence
//     EPD_2IN9_A1_ReadBusy(15 * 1000);
// }

void EPD_2IN9_A1_TurnOnDisplay(uint8_t part)
{
    EPD_2IN9_A1_SendCommand(0x22); // Display Update Control
    uint8_t reg;
    if (part = EPD_2IN9_A1_FULL)
        reg = 0xc4;
    else if (part = EPD_2IN9_A1_PART)
        reg = 0x04;
    else if (part = EPD_2IN9_A1_FAST)
        reg = 0xC7;
    // EPD_2IN9_A1_SendData(reg);
    // EPD_2IN9_A1_SendData(0xC4);
        EPD_2IN9_A1_SendData(0xC4);
    EPD_2IN9_A1_SendCommand(0x20); // Activate Display Update Sequence
    EPD_2IN9_A1_SendCommand(0xFF);
    EPD_2IN9_A1_ReadBusy(20 * 1000);
}

/******************************************************************************
function :	Sends the image buffer in RAM to e-Paper and displays
parameter:  color:black,0;red,1;*data;index;length
******************************************************************************/
void EPD_2IN9_A1_Data_Write(UBYTE color, UBYTE *Image_Data, UDOUBLE index, UDOUBLE length)
{
    if (index == 0)
    // if (1)
    {
        if (color == 0)
        {
            EPD_2IN9_A1_SendCommand(0x24);
        }
        else if (color == 1)
        {
            EPD_2IN9_A1_SendCommand(0x26);
        }
        DEV_Digital_Write(EPD_DC_PIN, 1);
    }

    UDOUBLE Width, Height;
    Width = (EPD_2IN9_A1_WIDTH % 8 == 0) ? (EPD_2IN9_A1_WIDTH / 8) : (EPD_2IN9_A1_WIDTH / 8 + 1);
    Height = EPD_2IN9_A1_HEIGHT;

    // if(length==(Width*Height))
    // {
    //     for (UDOUBLE j = 0; j < Height; j++)
    //     {
    //         for (UDOUBLE i = 0; i < Width; i++)
    //         {
    //             EPD_2IN9_A1_SendData(Image_Data[i + (Height-j) * Width]);
    //         }
    //     }

    // }
    // else{
#ifdef EPD_SPI_ENABLE
    EPD_2IN9_A1_SendData2(Image_Data, length);
#else
    for (UDOUBLE i = 0; i < length; i++)
    {
        if (color == 0)
        {
            // DEV_Digital_Write(EPD_DC_PIN, 1);
            // DEV_Digital_Write(EPD_CS_PIN, 0);
            EPD_2IN9_A1_SendData2(Image_Data[i]);
            // DEV_Digital_Write(EPD_CS_PIN, 1);
        }
        else if (color == 1)
        {
            // DEV_Digital_Write(EPD_DC_PIN, 1);
            // DEV_Digital_Write(EPD_CS_PIN, 0);
            EPD_2IN9_A1_SendData2(Image_Data[i]);
            // DEV_Digital_Write(EPD_CS_PIN, 1);
        }
    }
#endif
    // }
}

void EPD_2IN9_A1_setDepth(uint8_t i)
{
    iLut = i ? (i > 15 ? 15 : i) : 15;
}

#if 1
int8_t EPD_A1_ReadTemp()
{
    int8_t rc = 0;

    // EPD_2IN9_A1_SendCommand(0x12); // soft reset
    // EPD_SSD_WaitBusy(100);
    // EPD_2IN9_A1_Reset();

    // EPD_2IN9_A1_ReadBusy(100);
    EPD_2IN9_A1_SendCommand(0x12); // SWRESET
    // EPD_2IN9_A1_ReadBusy(1000);
    DEV_Delay_us(200);

    // Border Waveform
    EPD_2IN9_A1_SendCommand(0x3C);
    EPD_2IN9_A1_SendData(0x05);

    // Temperature sensor control
    EPD_2IN9_A1_SendCommand(0x18);
    EPD_2IN9_A1_SendData(0x80); // 80: internal sensor 48: external sensor

    // Display update control
    EPD_2IN9_A1_SendCommand(0x22);
    EPD_2IN9_A1_SendData(0xb1); // full: 0xf7

    // Master Activation
    EPD_2IN9_A1_SendCommand(0x20);
    // EPD_SSD_WaitBusy(100);
    EPD_2IN9_A1_ReadBusy();

    // read temperature
    EPD_2IN9_A1_SendCommand(0x1b);
    rc = EPD_SSD_ReadData();

    return rc;
}
#endif
